﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TS.Bussiness;
using TS.Commons;
using TS.Entities;
using Wuqi.Webdiyer;

namespace Tranco.Views
{
    public partial class NumberBikeManagement : System.Web.UI.Page
    {
        private int _pageSize; // Number record display on gridview
        private int _pageIndex; // Number page display
        private string _filter; // Condition filter 
        private string _orderBy; // Condition order follow header on gridview
        protected void Page_Load(object sender, EventArgs e)
        {
            _pageIndex = 1;
            _pageSize = 10;
            if (!IsPostBack)
            {
                _orderBy = HiddenFieldOrderBy.Value;
                _filter = HiddenFieldStringFilter.Value;
                BindData(_pageSize, _pageIndex, _filter, _orderBy);
            }

        }

        /// <summary>
        /// Bind Data
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="filter"></param>
        /// <param name="orderBy"></param>
        private void BindData(int pageSize, int pageIndex, string filter, string orderBy)
        {
            int totalRecord;
            var bussiness = new NumberBikeBussiness();
            Collection<NumberBike> collection = bussiness.ReadNumberBikePaged(pageSize, pageIndex, filter, orderBy, out totalRecord);
            if(collection == null) return;
            if (collection.Count == 0)
            {
                NumberBike user = new NumberBike();
                user.Description = "No Data";
                collection.Add(user);
            }
            GridViewList.DataSource = collection;
            GridViewList.DataBind();
            AspNetPager.RecordCount = totalRecord;
            AspNetPager.PageSize = pageSize;
        }

        /// <summary>
        /// Page Changing
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void AspNetPagerPageChanging(object src, PageChangingEventArgs e)
        {
            _pageIndex = e.NewPageIndex;
            _orderBy = HiddenFieldOrderBy.Value;
            if (!string.IsNullOrEmpty(_orderBy))
            {
                _orderBy = _orderBy + HiddenFieldStatusOrder.Value;
            }
            _filter = HiddenFieldStringFilter.Value;
            BindData(_pageSize, _pageIndex, _filter, _orderBy);
        }

        /// <summary>
        /// FilterData
        /// </summary>
        protected void DataFilter()
        {
            //get filter
            _filter = HiddenFieldStringFilter.Value;
            //get orderby
            _orderBy = HiddenFieldOrderBy.Value;

            if (!string.IsNullOrEmpty(_orderBy))
            {
                _orderBy = _orderBy + HiddenFieldStatusOrder.Value;
            }
            //set property of page
            _pageIndex = 1;
            AspNetPager.CurrentPageIndex = _pageIndex;
            //load data
            BindData(_pageSize, _pageIndex, _filter, _orderBy);
        }

        protected void GridViewListSorting(object sender, GridViewSortEventArgs e)
        {
            HiddenFieldOrderBy.Value = e.SortExpression;
            HiddenFieldStatusOrder.Value = HiddenFieldStatusOrder.Value.Equals(" ASC ") ? " DESC " : " ASC ";

            DataFilter();
        }

        private string UrlUpdate = "~/Views/UpdateNumberBike.aspx?nbid=";
        protected void GridViewListRowCommand(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                var result = e.CommandArgument.ToNumeric();
                if (result > 0)
                {
                    Response.Redirect(UrlUpdate + result);

                }
                //if (result.IsActive)
                //{
                //    Response.Redirect(UrlDirectorateEdit + e.CommandArgument + "&OrgId=" + HiddenFieldStringOrgIdDirec.Value);
                //}
                //else
                //{
                //    ClientConfirmDirec(e.CommandArgument.ToString());
                //}
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            HiddenFieldStringFilter.Value = txtFindUser.Text;

            DataFilter();
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridViewList.Rows)
            {
                CheckBox cbSelection = (CheckBox)row.FindControl("cbSelection");
                if (cbSelection.Checked)
                {
                    NumberBike user = new NumberBike();
                    LinkButton linkButton = (LinkButton)row.FindControl("lbtPlate");
                    user.NumberBikeId = linkButton.CommandArgument.ToNumeric();

                    NumberBikeBussiness ub = new NumberBikeBussiness();
                    if (ub.DeleteNumberBike(user) > 0)
                    {
                        ClientConfirm();
                        //Response.Redirect("~/Views/NumberBikeManagement.aspx");
                    }
                    else
                    {
                        ClientAlert("Có lỗi xảy ra. Vui lòng kiểm tra lại.");
                    }
                }
            }
        }

        /// <summary>
        /// Message alert
        /// </summary>
        /// <param name="strAlert"></param>
        public void ClientAlert(string strAlert)
        {
            var strScript = new StringBuilder();
            strScript.Append("<script language=JavaScript>");
            string scriptString = string.Format(CultureInfo.CurrentCulture, @"window.alert('" + strAlert + "');</script> ");
            strScript.Append(scriptString);
            ClientScript.RegisterStartupScript(GetType(), "Pop", strScript.ToString());
        }

        /// <summary>
        /// Message confirm
        /// </summary>
        public void ClientConfirm()
        {
            var strScript = new StringBuilder();
            strScript.Append("<script language=JavaScript>");
            string scriptString = string.Format(CultureInfo.CurrentCulture, @"var result = window.confirm('Xóa thành công. Bạn có muốn load lại trang?'); " +
                     "if(result) location.href='/Views/NumberBikeManagement.aspx';</script>");
            strScript.Append(scriptString);
            ClientScript.RegisterStartupScript(GetType(), "Pop", strScript.ToString());
        }
    }
}